package com.hanserwei.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class TTLQueueConfig {

    @Bean
    public Queue ttlQueue() {
        return new Queue("ttl.queue", true, false, false, Map.of(
                "x-message-ttl", 10000,
                "x-dead-letter-exchange", "ttl.dead.exchange",
                "x-dead-letter-routing-key", "ttl.dead.routing.key",
                "x-max-length", 5
        ));
    }

    @Bean
    public DirectExchange ttlExchange() {
        return new DirectExchange("ttl.exchange");
    }

    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(ttlQueue())
                .to(ttlExchange())
                .with("ttl.routing.key");
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue("ttl.dead.queue", true, false, false);
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("ttl.dead.exchange");
    }

    //把 DLQ 绑定到 DLX
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with("ttl.dead.routing.key");
    }
}

